package org.vectrics.swing;

import java.util.ArrayList;
import java.util.Iterator;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import java.awt.Color;
import org.apache.log4j.*;

public class TablePanel extends JPanel {

	private static final long serialVersionUID = 9008164481242652809L;

	private transient static Logger log = Logger.getLogger(TablePanel.class);

	private ArrayList rows = new ArrayList();	
	private int columnCount = 2;
	private int xPadding = 2;
	private int yPadding = 2;
	private Color cellColor = null;
	private int currentRow = -1;
	private int currentCol = 0;
	private ArrayList rowList = new ArrayList();
	private ArrayList columnWidths = new ArrayList();
	private int defaultColumnWidth = 150;
	private JPanel panel = new JPanel();
	private int topMargin = 5;
	private int leftMargin = 5;

	public TablePanel() {
		super();
		//this.setBackground(Color.cyan);
		//panel.setBackground(Color.MAGENTA);
		this.setLayout(new BorderLayout());
		panel.setLayout(null);
		this.add(panel, BorderLayout.CENTER);
	}


	public Row addRow() {
		log.debug("addRow() - method to add row of cells");
		Row row = new Row(this);
		rowList.add(row);
		return(row);
	}

	public void addColumnWidth(int width) {
		log.debug("addColumnWidth() - method to add a width for the next column");
		Integer widthInteger = new Integer(width);
		columnWidths.add(widthInteger);
	}
	

	public int getColumnLeft(int column) {
		log.debug("getColumnLeft() - returns the left pixel position of the passed column");
		int i = 0;
		int left = 2 + this.getXPadding() + this.getLeftMargin();
		while (i < column) {
			if (columnWidths.size() > column) {
				Integer columnWidth = (Integer)columnWidths.get(i);
				left += columnWidth.intValue();
			}
			else {
				left += defaultColumnWidth;
			}
			left += this.getXPadding();
			i++;
		}
		return(left);
	}

	
	protected int getRowY(Row row) {
		log.debug("getRowY() - returns the y pixel position of the passed row");
		int y = this.getYPadding() + this.getTopMargin();
		Iterator iter = rowList.iterator();
		boolean done = false;
		while ((done == false) && (iter.hasNext())) {
			Row curRow = (Row)iter.next();
			if (curRow == row) {
				done = true;
			}
			else {
				y += curRow.getHeight();
				y += this.getYPadding();
			}
			
		}
		
		return(y);
	}
	
	
	public int getColumnCount() {
		return columnCount;
	}


	public void setColumnCount(int columns) {
		this.columnCount = columns;
	}

	public int getXPadding() {
		return xPadding;
	}

	public void setXPadding(int padding) {
		xPadding = padding;
	}

	
	public int getYPadding() {
		return yPadding;
	}

	public void setYPadding(int padding) {
		yPadding = padding;
	}

	public int getRowCount() {
		return(this.rows.size());
	}

	public Color getCellColor() {
		return cellColor;
	}

	public void setCellColor(Color cellColor) {
		this.cellColor = cellColor;
	}

	/**
	 * @return Returns the panel.
	 */
	public JPanel getPanel() {
		return panel;
	}

	/**
	 * @return Returns the leftMargin.
	 */
	public int getLeftMargin() {
		return leftMargin;
	}

	/**
	 * @param leftMargin The leftMargin to set.
	 */
	public void setLeftMargin(int leftMargin) {
		this.leftMargin = leftMargin;
	}

	/**
	 * @return Returns the topMargin.
	 */
	public int getTopMargin() {
		return topMargin;
	}

	/**
	 * @param topMargin The topMargin to set.
	 */
	public void setTopMargin(int topMargin) {
		this.topMargin = topMargin;
	}

}
